/***************************************************************************
 *   This file is part of KTagger                                          *
 *   Copyright (C) 2010 by Luca Bellonda                                   *
 *   lbellonda _at_ gmail.com                                              *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#ifndef ASSIGN_TAG_WORKER_H
#define ASSIGN_TAG_WORKER_H

#include <QString>
#include <QStringList>
#include <QThread>
#include <QTimer>
#include <QDebug>

#include "config-ktagger.h"
#include "AppletLog.h"


class AssignTagsWorker : public QThread
{
    Q_OBJECT

public:
    AssignTagsWorker(QObject* parent = NULL);
    ~AssignTagsWorker();
    void run();
    void callDebug();
    void assignFiles(const QString &activityName, const QStringList & fileList);
    void configure();
    void assignOtherTags(const QString &activityName, const QString &resource);
    void chooseProfile();
    QStringList callGetTags(const QString &activityName);

signals:
    void processFinished(const bool isError, const int errorCode, const QString &errorMessage);
    void processProfileFinished(const bool isError, const int errorCode, const QString &errorMessage);
    void configurationFinished();
    void tick();

public slots:
    void updateProgress();

private:

    class Operation
    {
    protected:
        AssignTagsWorker *_owner;
    public:
        Operation(AssignTagsWorker *_owner);
        virtual ~Operation();
        virtual void start() = 0 ;
        virtual void end(const bool isError, const int errorCode, const QString &errorMessage) = 0 ;
    };

    class OperationDebug ;
    class OperationAssign ;
    class OperationAssignOtherTags ;
    class OperationConfigure ;
    class OperationChooseProfile;

    friend class OperationDebug ;
    friend class OperationAssign ;
    friend class OperationConfigure ;
    friend class OperationAssignOtherTags ;
    friend class OperationChooseProfile;

    QTimer _timer;
    LogParam    _logParams;
    bool    _isDebug;
    int _count ;
    bool _started;
    QStringList _fileList;
    QString _resource;
    Operation *_operation;
    QString _currentActivity;

    void init();
    void initLog();
    void endProcessing(const bool isError, const int errorCode, const QString &errorMessage);
    void callInterface();
    void xmitStrings();
    void callConfigure();
    void callAssignOtherTags();
    void callChooseProfile();
    void setOperation(Operation *operation);
    void clearOperation();
    AssignTagsWorker::Operation *allocOperation(Operation *operation);
    void emitProcessFinished(const bool isError, const int errorCode, const QString &errorMessage);
    void emitProcessProfileFinished(const bool isError, const int errorCode, const QString &errorMessage);

};


#endif /* ASSIGN_TAG_WORKER_H */
